import os
import sys
import glob
from export.Logger import Logger
from export.utils import gen_pass

# 配置日志文件
BASE_DIR, fn = os.path.split(__file__)
logfile = os.path.join(BASE_DIR, fn.split('.')[0] + '.log')
logging = Logger(logfile, level='debug').logger


def create_zip(source_file_path, zipname):
    # 加密软件minizip路径
    zip_cmd = '"C:\\7-Zip\\7z.exe"'
    # 待压缩文件路径
    sourcefile_path = os.path.join(source_file_path, 'exports')
    # 压缩包保存路径
    zip_path = os.path.join(source_file_path, 'zipfiles')

    # 先清理之前的压缩文件
    logging.info('开始清理zip文件')
    zipfile = glob.glob(os.path.join(zip_path, '*.zip'))
    for zf in zipfile:
        os.remove(zf)

    # 压缩文件取名
    pa = gen_pass()  # 生成随机密码
    zipname = zipname + '_' + pa + '.zip'

    # 生成压缩包
    logging.info('生成压缩文件:{}'.format(os.path.join(zip_path, zipname)))
    cmd = "{} a -ssw -p{} {} {}".format(zip_cmd, pa, os.path.join(zip_path, zipname), sourcefile_path)
    os.system(cmd)

    # 执行完后删除导出的文件
    exports_file = glob.glob(os.path.join(sourcefile_path, '*.*'))
    for z in exports_file:
        logging.info('删除导出的文件:{}'.format(z))
        os.remove(z)

    return os.path.join(zip_path, zipname)


if __name__ == '__main__':
    create_zip('C:\\Users\\xdzf-tf\\PycharmProjects\\mail-issue\\export\\task\\BI系统\\20210303\\风控\\二维码', '20210309104')
